package com.ygo.feihua;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.ygo.feihua.BmobTable.MessageAlert;
import java.util.List;
import android.support.v7.widget.Toolbar;
import cn.bmob.v3.BmobQuery;
import com.ygo.feihua.BmobTable.MyUser;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.exception.BmobException;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.view.View;
import android.util.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.util.*;
import cn.bmob.v3.*;
import cn.bmob.v3.listener.*;
import cn.bmob.v3.datatype.*;
import java.util.*;

public class ShequMessageAlert extends AppCompatActivity
{

	private ListView sqme_list;
	private Gongju gj;
	private List<MessageAlert> data;
	private UserManagement um;
	PlAdapter pla;
	private DialogUtils du;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shequ_message);
		csh();
		sqme_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					String id =data.get(p3).getHftie().getObjectId();	
					if(id==null){
						gj.show("没有该帖子的信息");
					}else{
						gj.tieziIntent(id);
					}
					
					// TODO: Implement this method
				}
			});
		
	}
	private void csh(){
		sqme_list=(ListView) findViewById(R.id.sqme_list);
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		um=UserManagement.getDx();
		gj.cshToolbar(tool,"消息提醒");
		
		listcsh();
	}
	private void listcsh(){
		du.dialogj1(false,"","加载中,请稍等");
		BmobQuery<MessageAlert> bq=new BmobQuery<MessageAlert>();
		bq.order("-createdAt");
		bq.addWhereEqualTo("hfuser",um.getUser());
		bq.addWhereNotEqualTo("senduser",um.getUser());
		bq.include("senduser,hftie,hfhf");
		bq.findObjects(new FindListener<MessageAlert>(){

				@Override
				public void done(List<MessageAlert> p1, BmobException p2)
				{
					if(p2==null){
						data=p1;
						pla=new PlAdapter(ShequMessageAlert.this,data);
						
						List<BmobObject> lb=new ArrayList<BmobObject>();
						for(MessageAlert ma:p1){
							if(ma.getIsShow()==null||!ma.getIsShow()){
							MessageAlert ml=new MessageAlert();
							ml.setObjectId(ma.getObjectId());
							ml.setIsShow(true);
							lb.add(ml);
							}
						}
						
						
						new BmobBatch().updateBatch(lb).doBatch(new QueryListListener<BatchResult>() {

								@Override
								public void done(List<BatchResult> o, BmobException e) {
									
								}
							});
						
						sqme_list.setAdapter(pla);
						du.dis();
					}else{
						du.dis();
						gj.show("查询失败,原因为"+p2);
						//gj.toastBmobException(sqme_list,"查询失败",p2);
						finish();
					}
					// TODO: Implement this method
				}
			});
		
		
	}
	
}
